/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
*/

package com.jinhe.tss.cms.action;

import java.util.List;
import java.util.Map;

import com.jinhe.tss.cms.CMSConstants;
import com.jinhe.tss.cms.entity.Channel;
import com.jinhe.tss.cms.publish.PublishManger;
import com.jinhe.tss.cms.service.IChannelService;
import com.jinhe.tss.cms.workflow.WorkFlowManager;
import com.jinhe.tss.component.dynproperty.support.IRemotePropertyService;
import com.jinhe.tss.component.support.web.webwork.ProgressActionSupport;
import com.jinhe.tss.core.exception.BusinessException;
import com.jinhe.tss.core.util.XMLDocUtil;
import com.jinhe.tss.core.web.dispaly.tree.LevelTreeParser;
import com.jinhe.tss.core.web.dispaly.tree.TreeEncoder;
import com.jinhe.tss.core.web.dispaly.xform.XFormEncoder;
 
public class ChannelAction extends ProgressActionSupport {

	private static final long serialVersionUID = -8424497703591664847L;
    
	private IChannelService  channelService;
	private PublishManger    publishManger;
	private IRemotePropertyService propertyService;
    
    private Channel channel = new Channel(); // 栏目信息
    
    private Long    parentId;
	private Long    channelId;    // 栏目编号
	private Long    toChannelId; // 排序到的栏目ID
	private Integer direction;  // 排序方向  -1目标上方,1目标下方
	
	private String  distributeFromIds; // 栏目的关系源id
	private String  distributeToIds;   // 栏目的目的源id
	private String  category; // 1:增量发布  2:完全发布

	/**
	 * 获取所有的栏目树结构
	 */
	public String getChannelAll() {
		List<?> list = channelService.getAllChannels();
		TreeEncoder channelTreeEncoder = new TreeEncoder(list, new LevelTreeParser());
		channelTreeEncoder.setNeedRootNode(false);
		return print("ChannelTree", channelTreeEncoder);
	}

	/**
	 * 获取栏目xform信息
	 */
	public String getChannelDetail() {
		if ( isCreateNew() ) {
            channel = new Channel();
            
            Channel parent = (Channel) channelService.getChannelById(parentId);
            channel.setArticleTypeId(parent.getArticleTypeId());
            channel.setArticleTypeName(parent.getArticleTypeName());
            channel.setHotArticleRule(parent.getHotArticleRule());
            channel.setNewArticleRule(parent.getNewArticleRule());
            channel.setWorkflowId(parent.getWorkflowId());
            channel.setWorkflowName(parent.getWorkflowName());
            channel.setOverdueDate(parent.getOverdueDate());
            channel.setIsSite(CMSConstants.FALSE);
            channel.setSiteId(parent.isSite() ? parent.getId() : parent.getSiteId());
            channel.setParentId(parentId);
		} 
		else {
			channel = channelService.getChannelById(channelId);
		}
        
		Map<String, String> map = propertyService.getXFormTemplate("channel", "baseXForm4Channel");
		Object template = map.get(CMSConstants.CHANNEL_BASE_INFO_TAB_KEY);
        if( template == null ) {
			throw new BusinessException("栏目表单布局尚未设置,请转到动态属性进行设置.");
        }
		XFormEncoder xEncoder = new XFormEncoder(XMLDocUtil.dataXml2Doc((String) template), channel, false, false);

        String[] objs = WorkFlowManager.getInstance().getWorkFlowCodeAndNames();
        xEncoder.setColumnAttribute("workflowId", "editorvalue", objs[0]);
        xEncoder.setColumnAttribute("workflowId", "editortext", objs[1]);
		return print("ChannelInfo", xEncoder);
	}

	/**
	 * 新增栏目
	 */
	public String saveChannel() {
	    channelService.createChannel(channel);
		return doAfterSave(true, channel, "ChannelTree");
	}

	/**
	 * 更新栏目
	 */
	public String updateChannel() {
		channelService.updateChannel(channel);
		return printSuccessMessage("修改成功！");
	}

	/**
	 * 逻辑删除栏目
	 */
	public String deleteChannel() {
		channelService.deleteChannel(channelId);
        return printSuccessMessage("删除成功！");
	}

	/**
	 * 栏目排序
	 */
	public String sortChannel() {
		channelService.sortChannel(channelId, toChannelId, direction);
        return printSuccessMessage("排序成功！");
	}

	/**
	 * 栏目移动
	 */
	public String moveChannel() {
		channelService.moveChannel(channelId, toChannelId);
        return printSuccessMessage("移动成功！");
	}

	/**
	 * 实现栏目的复制和复制到的功能
	 */
	public String copyChannel(){
		List<Channel> list = channelService.copyChannel(channelId, toChannelId);
		TreeEncoder encoder = new TreeEncoder(list, new LevelTreeParser());
		encoder.setNeedRootNode(false);
		return print("ChannelTree", encoder);
	}

	/**
	 * 获取栏目的关系信息
	 */
	public String getChannelRelationShip() {
		Object[] objs = channelService.getChannelRelationShip(channelId);
		return print(new String[]{"SiteTree", "ChannelDistributeFrom", "ChannelDistributeTo"}, objs);
	}

	/**
	 * 保存栏目关系信息
	 */
	public String saveChannelRelationShip() {
		channelService.saveChannelRelationShip(channelId, distributeFromIds, distributeToIds);
		return printSuccessMessage("保存成功！");
	}

	/**
	 * 带有进度条的栏目发布
	 */
	public String publishChannel() {
        String code = publishManger.publishArticle(channelId, category);
        return printScheduleMessage(code);  
	}
    /**
     *  带有进度条的站点发布
     */
    public String publishSite(){
        String code = publishManger.publishArticle(channelId, category);
        return printScheduleMessage(code);  
    }

    
    public Channel getChannel() {  return channel; }

    public void setDistributeFromIds(String distributeFromIds) {
        this.distributeFromIds = distributeFromIds;
    }
    public void setDistributeToIds(String distributeToIds) {
        this.distributeToIds = distributeToIds;
    }
    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    public void setDirection(Integer direction) {
        this.direction = direction;
    }
    public void setToChannelId(Long toChannelId) {
        this.toChannelId = toChannelId;
    }
    public void setChannelId(Long channelId) {
        this.channelId = channelId;
    }
    
    public void setChannelService(IChannelService channelService) {
        this.channelService = channelService;
    }
	public void setPropertyService(IRemotePropertyService propertyService) {
		this.propertyService = propertyService;
	}
	public void setPublishManger(PublishManger publishManger) {
		this.publishManger = publishManger;
	}
}
